# TODO: improve this parsing using regular expressions
def parse_datetime(s):
	if s.find('T') == -1:
		# just date
		fmt = '%Y-%m-%d'
		t = s
	elif s.find('.') != -1:
		# datetime and possibly msecs
		fmt = '%Y-%m-%dT%H:%M:%S'
		t = s[:s.rindex('.')]
	elif s.find('Z') != -1:
		# datetime with timezone
		fmt = '%Y-%m-%dT%H:%M:%S'
		t = s[:s.rindex('Z')]
	elif len(s) == 19:
		fmt = '%Y-%m-%dT%H:%M:%S'
		t = s
	else:
		raise Exception('Unsupported datetime format (%s).' % s)

	import time
	
	return int(time.mktime(time.strptime(t, fmt)))

